package com.dawn.module.hospital.service.ehealth;

import com.alibaba.fastjson.JSONObject;
import com.tencent.healthcard.impl.HealthCardServerImpl;
import com.tencent.healthcard.model.CommonIn;
import com.tencent.healthcard.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.TreeMap;

/**
 * @author cnsu-cmh
 * @date 2024-08-24 11:01
 * @description
 */
public class DawnHealthCardServerImpl extends HealthCardServerImpl {

    static final String SSM_GEN_QR_CODE_URL = "https://p-healthopen.tengmed.com/rest/auth/TXHealthCard/EHealthCardServer/ToolsObj/ssmGenQrCode";
    static final String SSM_QUERY_QR_CODE_RESULT_URL = "https://p-healthopen.tengmed.com/rest/auth/TXHealthCard/EHealthCardServer/ToolsObj/ssmQueryQrCodeResult";
    static final String GET_HEALTH_CARD_BY_ID_URL = "https://p-healthopen.tengmed.com/rest/auth/TXHealthCard/EHealthCardServer/ISVOpenObj/getHealthCardByHealthCardId";

    static final String MACHINE_GEN_QR_CODE_URL = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/MachineSelfServer/MachineSelfServerObj/machineGenQrCode";
    static final String MACHINE_QUERY_QR_CODE_RESULT_URL = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/MachineSelfServer/MachineSelfServerObj/machineQueryQrCodeResult";

    static final String REGISTER_UNIFORM_VERIFY_ORDER_URL = "https://p-healthopen.tengmed.com/rest/auth/HealthCard/HealthOpenPlatform/ISVOpenObj/registerUniformVerifyOrder";

    static final String CHECK_UNIFORM_VERIFY_RESULT_URL = "https://p-healthopen.tengmed.com/rest/auth/TXHealthCard/EHealthCardServer/ISVOpenObj/checkUniformVerifyResult";


    public DawnHealthCardServerImpl(String secret) {
        super(secret);
    }

    public DawnHealthCardServerImpl(String secret, int connectTimeout, int readTimeout) {
        super(secret, connectTimeout, readTimeout);
    }

    public JSONObject ssmGenQrCode(CommonIn commonIn, String ssmId, String notifyUrl) {
        return this.ssmGenQrCode(commonIn, ssmId, notifyUrl, super.getConnectTimeout(), super.getReadTimeout());
    }

    public JSONObject ssmGenQrCode(CommonIn commonIn, String ssmId, String notifyUrl, int connectTimeout, int readTimeout) {
        Map<String, Object> param = new TreeMap();
        param.put("ssmId", ssmId);
        if(StringUtils.isNotBlank(notifyUrl)) {
            param.put("notifyUrl", notifyUrl);
        }
        String reqJson = CommonUtil.packParam(super.getSecret(), commonIn, param, super.isLog());
        return this.request(SSM_GEN_QR_CODE_URL, reqJson, connectTimeout * 1000, readTimeout * 1000);
    }


    public JSONObject ssmQueryQrCodeResult(CommonIn commonIn, String uid) {
        return this.ssmQueryQrCodeResult(commonIn, uid,super.getConnectTimeout(), super.getReadTimeout());
    }

    public JSONObject ssmQueryQrCodeResult(CommonIn commonIn, String uid, int connectTimeout, int readTimeout) {
        Map<String, Object> param = new TreeMap();
        param.put("uid", uid);
        String reqJson = CommonUtil.packParam(super.getSecret(), commonIn, param, super.isLog());
        return this.request(SSM_QUERY_QR_CODE_RESULT_URL, reqJson, connectTimeout * 1000, readTimeout * 1000);
    }

    public JSONObject getHealthCardByHealthCardId(CommonIn commonIn, String healthCardId) {
        return this.getHealthCardByHealthCardId(commonIn, healthCardId,super.getConnectTimeout(), super.getReadTimeout());
    }

    public JSONObject getHealthCardByHealthCardId(CommonIn commonIn, String healthCardId,int connectTimeout, int readTimeout) {
        Map<String, Object> param = new TreeMap();
        param.put("healthCardId", healthCardId);
        String reqJson = CommonUtil.packParam(super.getSecret(), commonIn, param, super.isLog());
        return this.request(GET_HEALTH_CARD_BY_ID_URL, reqJson, connectTimeout * 1000, readTimeout * 1000);
    }

    public JSONObject machineGenQrCode(CommonIn commonIn, String ssmId, String notifyUrl) {
        return this.machineGenQrCode(commonIn, ssmId, notifyUrl, super.getConnectTimeout(), super.getReadTimeout());
    }

    public JSONObject machineGenQrCode(CommonIn commonIn, String ssmId, String notifyUrl, int connectTimeout, int readTimeout) {
        Map<String, Object> param = new TreeMap();
        param.put("machineId", ssmId);
        if(StringUtils.isNotBlank(notifyUrl)) {
            param.put("notifyUrl", notifyUrl);
        }
        String reqJson = CommonUtil.packParam(super.getSecret(), commonIn, param, super.isLog());
        return this.request(MACHINE_GEN_QR_CODE_URL, reqJson, connectTimeout * 1000, readTimeout * 1000);
    }

    public JSONObject machineQueryQrCodeResult(CommonIn commonIn, String uid) {
        return this.machineQueryQrCodeResult(commonIn, uid,super.getConnectTimeout(), super.getReadTimeout());
    }

    public JSONObject machineQueryQrCodeResult(CommonIn commonIn, String uid, int connectTimeout, int readTimeout) {
        Map<String, Object> param = new TreeMap();
        param.put("uid", uid);
        String reqJson = CommonUtil.packParam(super.getSecret(), commonIn, param, super.isLog());
        return this.request(MACHINE_QUERY_QR_CODE_RESULT_URL, reqJson, connectTimeout * 1000, readTimeout * 1000);
    }

    public JSONObject registerUniformVerifyOrder(CommonIn commonIn, String cardType, String idCard, String name, int connectTimeout, int readTimeout) {

        Map<String, Object> param = new TreeMap();
        param.put("cardType", cardType);
        param.put("idCard", idCard);
        param.put("name", name);

        String reqJson = CommonUtil.packParam(super.getSecret(), commonIn, param, super.isLog());
        return this.request(REGISTER_UNIFORM_VERIFY_ORDER_URL, reqJson, connectTimeout * 1000, readTimeout * 1000);
    }

    public JSONObject checkUniformVerifyResult(CommonIn commonIn, String verifyOrderId, String registerOrderId, int connectTimeout, int readTimeout) {
        Map<String, Object> param = new TreeMap();
        param.put("verifyOrderId", verifyOrderId);
        param.put("verifyResult", registerOrderId);

        String reqJson = CommonUtil.packParam(super.getSecret(), commonIn, param, super.isLog());
        return this.request(CHECK_UNIFORM_VERIFY_RESULT_URL, reqJson, connectTimeout * 1000, readTimeout * 1000);
    }
}
